﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace SmartMotion.Core
{
    public class PointsMgr
    {
        #region 单例

        private static readonly Lazy<PointsMgr> Instance = new Lazy<PointsMgr>(() => new PointsMgr());
        private string _filePath = string.Empty;

        private PointsMgr()
        {
            Open();
        }

        public static PointsMgr Ins { get; } = Instance.Value;

        #endregion 单例

        #region 属性

        public string FilePath
        {
            get
            {
                _filePath = ProductMgr.Ins.FilePath.Replace("prt", "points");
                return _filePath;
            }
        }

        public string ProductName { get; set; }

        //点位

        private ConcurrentDictionary<string, VarInfo> _points;

        public ConcurrentDictionary<string, VarInfo> Points
        {
            get
            {
                _points = GlobalVars.Ins.Vars[GlobalVars.Points];
                return _points;
            }
        }

        #endregion 属性

        public void Open()
        {
            GlobalVars.Ins.LoadDic(GlobalVars.Points, FilePath, false);
        }

        public void Save()
        {
            GlobalVars.Ins.SaveDic(GlobalVars.Points, FilePath);
        }

        public bool AddPoints(string name, string station, double[] position)
        {
            return Points.TryAdd($"{station}_{name}", new VarInfo()
            { Name = name, Type = typeof(double[]).FullName, Group = station, Value = position });
        }

        public List<VarInfo> GetPoints(string station)
        {
            return (from varInfo in Points where varInfo.Value.Group == station select varInfo.Value).ToList();
        }
    }
}